package com.accp.service.impl;

import com.accp.entity.Comments;
import com.accp.entity.Hotevents;
import com.accp.entity.Users;
import com.accp.mapper.CommentsMapper;
import com.accp.mapper.HoteventsMapper;
import com.accp.mapper.UsersMapper;
import com.accp.service.CommentsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jack
 * @since 2021-08-03
 */
@Service
public class CommentsServiceImpl  implements CommentsService {

	@Autowired
	CommentsMapper commentsMapper;
	
	@Autowired
	HoteventsMapper hoteventsMapper;
	
	@Autowired
	UsersMapper userMapper;
	
	@Override
	public List<Comments> getCommentsByHid(int hotEventId) {
		QueryWrapper<Comments> qu = new QueryWrapper<Comments>();
		qu.eq("hotEventsId", hotEventId);
		qu.orderByDesc("commentDate");
		List<Comments> comments = this.commentsMapper.selectList(qu);
		//查询评论人的对象
		for(Comments com : comments){
			Users user = this.userMapper.selectById(com.getUid());
			user.setPassword(null);
			com.setUser(user);
		}
		return comments;
	}

	@Override
	@Transactional
	public int addComments(Comments comments) {
		if(comments.getContent().length()<=2){
			throw new RuntimeException("内容太少了");
		}
		
		int count = this.commentsMapper.insert(comments);
		if(count == 1){
			Hotevents event = this.hoteventsMapper.selectById(comments.getHotEventsId());
			event.setSearchSum(event.getSearchSum()+1);
			this.hoteventsMapper.updateById(event);
		}
		
		return count;
	}

}
